/**
 * @file  MiraInFixIterator.h
 * @author Josep Sabater (jsabaterm@el.ub.edu)
 * @date   August, 2016
 * @version 1.0.0
 * @ingroup MiraUtilUBLib
 * @brief Iterator that adds an special character between values.
 *
 * Infix iterator lifted from Jerry Coffin's 's prefix_ostream_iterator.
 * and custom modifications added.
 */

#ifndef MIRA_INFIX_ITERATOR_H_
#define  MIRA_INFIX_ITERATOR_H_

#include <ostream> 
#include <iterator> 

namespace MiraUtilUBLib {
    template <class T, 
              class charT=char, 
              class traits=std::char_traits<charT> > 
    class InfixOstreamIterator : 
        public std::iterator<std::output_iterator_tag,void,void,void,void> { 
        std::basic_ostream<charT,traits> *os; 
        charT const* delimiter;
        bool first_elem;
        
    public: 
        typedef charT char_type; 
        typedef traits traits_type; 
        typedef std::basic_ostream<charT,traits> ostream_type; 
        
        InfixOstreamIterator(ostream_type& s) 
            : os(&s), delimiter(0), first_elem(true) {} 
        InfixOstreamIterator(ostream_type& s, charT const *d) 
            : os(&s), delimiter(d), first_elem(true) {}
       
        InfixOstreamIterator<T,charT,traits>& operator=(T const &item) { 
            // Here's the only real change from ostream_iterator: 
            // Normally, the '*os << item;' would come before the 'if'. 
            if (!first_elem && delimiter != 0) 
                *os << delimiter; 
            *os << item; 
            first_elem = false; 
            return *this; 
        } 
        
        InfixOstreamIterator<T,charT,traits> &operator*() { return *this; } 
        InfixOstreamIterator<T,charT,traits> &operator++() { return *this; } 
        InfixOstreamIterator<T,charT,traits> &operator++(int) { return *this; } 
    };  
}

#endif /*MIRA_INFIX_ITERATOR_H_*/